
function getSecurityKeyboard(url,t,windowWidth,windowHeight){
    var securityKeyboard={createSecurityKeyboard:standardSecurityKeyboard,createLetterKeyboard:createLetterKeyboard,pwd:new Array(),createExtendNumberKeyboard:createExtendNumberKeyboard};
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      data: {
        type: t ,
        r: Math.random()
      },
      header: {
          'content-type': 'application/x-www-form-url-encoded'
      },
      success: function(res) {
        var data = res.data.split("!!");
        securityKeyboard.UUID=data[1];
        securityKeyboard.key=data[2];
        securityKeyboard.createSecurityKeyboard(data[0],windowWidth,windowHeight);
        securityKeyboard.keyMap=securityKeyboard["yLetterImageKey_map"];

      }
    });
    return securityKeyboard;
}
function getPwd(securityKeyboard){
 
  var pwd=new Array();
  for(var i=0;i<securityKeyboard.pwd.length;i++){
    pwd.push(securityKeyboard.pwd[i].join("")+"!*!"+securityKeyboard.UUID);
  }
  return pwd;
}
function standardSecurityKeyboard(image,windowWidth,windowHeight){
	var timage=image.split(";$;");
	this.yLetterImage=timage[0];
	this.yLetterImageKey=timage[1];
	this.XLetterImage=timage[2];
	this.XLetterImageKey=timage[3];
	this.numberKeyImage=timage[4];
	this.numberKeyImageKey=timage[5];
	this.symbolImage=timage[6];
	this.symbolImageKey=timage[7];
  
  this.createLetterKeyboard("y",windowWidth,windowHeight);
  this.createLetterKeyboard("X",windowWidth,windowHeight);
  this.createExtendNumberKeyboard(windowWidth,windowHeight);
}

function showStandardSecurityKeyboardView(event,wxw){
    console.log(this);
    wxw.setData({
      securityKeyboard_image_src:"data:image/png;base64,"+wxw.data.securityKeyboard.yLetterImage,
      securityKeyboard_image_height:wxw.data.systemInfo.windowHeight*0.41,
      securityKeyboard_image_top:wxw.data.systemInfo.windowHeight-(wxw.data.systemInfo.windowHeight*0.41)
    });
  }
function securityKeyboard_Touchstart(event,wxw){
  console.log(event.touches[0]);
  var top=wxw.data.securityKeyboard_image_top;
  var cy=event.touches[0].clientY-top;
  console.log(top+"|||"+cy);
  
  if(wxw.data.securityKeyboard.keyMap == undefined){
    init(wxw.data);
  }
  if(wxw.data.securityKeyboard.pwd[wxw.data.securityKeyboard_input_index] == undefined){
      wxw.data.securityKeyboard.pwd[wxw.data.securityKeyboard_input_index]=new Array();
  }
  for(var name in wxw.data.securityKeyboard.keyMap){
    console.log(name);
    if(isinside(event.touches[0].clientX,cy,wxw.data.securityKeyboard.keyMap[name])){
      if(name==-4){
        wxw.setData({
          securityKeyboard_image_src:"data:image/png;base64,"+wxw.data.securityKeyboard.XLetterImage
        });
        wxw.data.securityKeyboard.keyMap=wxw.data.securityKeyboard["XLetterImageKey_map"];
        break;
      }else if(name==-1){
        wxw.setData({
          securityKeyboard_image_top:1000
        });
        break;
      }else if(name==-3){
        wxw.setData({
          securityKeyboard_image_src:"data:image/png;base64,"+wxw.data.securityKeyboard.yLetterImage
        });
        wxw.data.securityKeyboard.keyMap=wxw.data.securityKeyboard["yLetterImageKey_map"];
        break;
      }else if(name==-5){
        wxw.setData({
          securityKeyboard_image_src:"data:image/png;base64,"+wxw.data.securityKeyboard.numberKeyImage
        });
        wxw.data.securityKeyboard.keyMap=wxw.data.securityKeyboard["numberKeyImage_map"];
        break;
      }else if(name==-2){
        wxw.data.securityKeyboard.pwd[wxw.data.securityKeyboard_input_index].pop();
        var txt="";
        for(var i=0;i<wxw.data.securityKeyboard.pwd[wxw.data.securityKeyboard_input_index].length;i++){
          txt+="*";
        }
        var o=wxw.data.securityKeyboard_input;
        o[wxw.data.securityKeyboard_input_index]=txt;
        wxw.setData({
          securityKeyboard_input:o
        });
        break;
      }else{
        var o=wxw.data.securityKeyboard_input;
        o[wxw.data.securityKeyboard_input_index]=o[wxw.data.securityKeyboard_input_index]+"*";
        wxw.data.securityKeyboard.pwd[wxw.data.securityKeyboard_input_index].push(name);
        wxw.setData({
          securityKeyboard_input:o
        });
        break;
      }
    }
  }
  console.log(JSON.stringify(wxw.data.securityKeyboard.pwd));
}
function createExtendNumberKeyboard(windowWidth,windowHeight){
  this["numberKeyImage_map"]=new Object();
  var key=this["numberKeyImageKey"];
  var width=windowWidth/5.4;
	var height=parseInt(windowHeight*0.41)/4.45;
	var bleft=windowWidth/48;
  
	var jleft=windowWidth/130;
	var jtop=parseInt(windowHeight*0.41)/55;

var  s= "";
	for(var i = 0 ; i < 2 ; i++ ){
		for(var j = 0 ; j < 5 ; j++ ){
		  this["numberKeyImage_map"][key[(i*5)+j]]={jtop:((i+1)*jtop+(i*height)),left:(bleft+((j*jleft)+(j*width))),width:width,height:height};
    console.log("n\t\t"+key[(i*5)+j]+"|||"+JSON.stringify(this["numberKeyImage_map"][key[(i*5)+j]]));
    s+=key[(i*5)+j];
		}
    s+="\r\n";
	}
  console.log(s);
  
	var left=(windowWidth*0.865);
	width=windowWidth/8;
	this["numberKeyImage_map"][-2]={jtop:((jtop*3)+(height*2)),left:left,width:width,height:height};
  left=(windowWidth/95);
	width=windowWidth/4;
  this["numberKeyImage_map"][-3]={jtop:((jtop*4)+(height*3)),left:left,width:width,height:height};
  left=(windowWidth/1.35);
	width=windowWidth/4;
  this["numberKeyImage_map"][-1]={jtop:((jtop*4)+(height*3)),left:left,width:width,height:height};

}
function createLetterKeyboard(c,windowWidth,windowHeight){
  this[c+"LetterImageKey_map"]=new Object();
	var key=this[c+"LetterImageKey"];
	var width=windowWidth/10.6;
	var height=windowHeight*0.41/4.4;
	var jleft=windowWidth/153
	var jtop=windowHeight*0.41/55;

  var  s= "";
	for(var i=0;i<10;i++){
		var left=i*width+(i*jleft);
		this[c+"LetterImageKey_map"][key[i]]={jtop:0,left:left,width:width,height:height};
    console.log(key[i]+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][key[i]]));
    s+=key[i];
	}
  s+="\r\n";
  for(var i=0;i<9;i++){
		var left=(windowWidth/20)+i*width+(i*jleft);
    this[c+"LetterImageKey_map"][key[i+10]]={jtop:((jtop*2)+height),left:left,width:width,height:height};
    console.log(key[i+10]+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][key[i+10]]));
    s+=key[i+10];
	}
  s+="\r\n";
	for(var i=0;i<7;i++){
    var left=(windowWidth/6.7)+i*width+(i*jleft);
    this[c+"LetterImageKey_map"][key[i+19]] ={jtop:((jtop*3)+(height*2)),left:left,width:width,height:height};
    console.log(key[i+19]+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][key[i+19]]));
    s+=key[i+19];
	}
  console.log(s);
  var left=(windowWidth/95);
	width=windowWidth/8;
  if(c=="y"){
    this[c+"LetterImageKey_map"][-4] = {jtop:((jtop*3)+(height*2)),left:left,width:width,height:height};
  }else{
    this[c+"LetterImageKey_map"][-3] = {jtop:((jtop*3)+(height*2)),left:left,width:width,height:height};
  }
  
    console.log(-4+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][-4]));
	left=(windowWidth*0.865);
	width=windowWidth/8;  
  this[c+"LetterImageKey_map"][-2] = {jtop:((jtop*3)+(height*2)),left:left,width:width,height:height};
    console.log(-2+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][-2]));
	left=(windowWidth/95);
	width=windowWidth/4;
  this[c+"LetterImageKey_map"][-5] = {jtop:((jtop*4)+(height*3)),left:left,width:width,height:height};
    console.log(-5+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][-5]));
	
	left=(windowWidth/1.35);
	width=windowWidth/4;
  this[c+"LetterImageKey_map"][-1] = {jtop:((jtop*4)+(height*3)),left:left,width:width,height:height};
    console.log(-1+"|||"+JSON.stringify(this[c+"LetterImageKey_map"][-1]));
}

function isinside(clientX,clientY,key){
  var y=key["jtop"];
  var x=key["left"];
  var width=key["width"];
  var height=key["height"];
  
  console.log(clientX+"\t"+clientY+"\t"+y+"\t"+x+"\t"+JSON.stringify(key));
  if(clientX > x && clientX < x+width && clientY > y && clientY < y+height){
    return true;
  }else{
    return false;
  }
}

module.exports = {
  getSecurityKeyboard: getSecurityKeyboard,
  showStandardSecurityKeyboardView:showStandardSecurityKeyboardView,
  securityKeyboard_Touchstart:securityKeyboard_Touchstart,
  getPwd:getPwd
}
